package pub.dsb.example.rx.core;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.MaybeObserver;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version V1.0.0
 * @date 2020/12/24 15:01
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2020/12/24 15:01
 * @modify reason: {方法名}:{原因}
 * ...
 * @since V1.0.0
 */
public class MaybeTest {

    /**
     * Maybe
     *
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("DEMO 1 -------------");
        Maybe.<Integer>create(sub -> {
            sub.onSuccess(1);
            //sub.onError(new IllegalAccessException("error!")); 异常直接失败
            sub.onComplete();
        }).subscribe(new MaybeObserver<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("onSubscribe");
            }

            @Override
            public void onSuccess(@NonNull Integer v) {
                System.out.println("onSuccess: " + v);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                System.out.println(e.getMessage());
            }

            @Override
            public void onComplete() {
                // 无数据发送时候的onComplete事件
                System.out.println("onComplete");
            }
        });

        System.out.println("DEMO 2 -------------");
        Maybe.<Integer>create(sub -> {
            //sub.onSuccess(1);
            //sub.onError(new IllegalAccessException("error!")); 异常直接失败
            sub.onComplete();
        }).subscribe(new MaybeObserver<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("onSubscribe");
            }

            @Override
            public void onSuccess(@NonNull Integer v) {
                System.out.println("onSuccess: " + v);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                System.out.println(e.getMessage());
            }

            @Override
            public void onComplete() {
                // 无数据发送时候的onComplete事件
                System.out.println("onComplete");
            }
        });
        System.out.println("DEMO 3-------------");
        // 模拟数据统计
        Maybe.<Long>just(count())
                //可能涉及到IO操作，放在子线程
                .subscribeOn(Schedulers.newThread())
                //取回结果：当其它排队的任务完成后，在当前线程排队开始执行
                .observeOn(Schedulers.trampoline())
                .subscribe(new MaybeObserver<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        System.out.println("onSubscribe");
                    }

                    @Override
                    public void onSuccess(@NonNull Long v) {
                        System.out.println("onSuccess: " + v);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        System.out.println(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        // 无数据发送时候的onComplete事件
                        System.out.println("onComplete");
                    }
                });

        try {
            // 阻塞等待主线程结果
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 模拟数据库数据统计
     * @return
     */
    private static Long count() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return System.currentTimeMillis();
    }
}
